''微信公众号关注：VB小源码
''QQ群：344402874
''博客：https://www.cnblogs.com/vbee/
''个人微信：vbee_club
Imports System.Security.Cryptography
Imports System.Text

Public Class Cls_RSA

  ''' <summary>
  ''' 生成公钥,密钥
  ''' </summary>
  ''' <returns></returns>
  Public Shared Function Create_pub_pri_key(Optional size As Integer = 1024) As Create_key_type
    Dim key As New Create_key_type
    Using rsa As New RSACryptoServiceProvider(size) ' 384 位到 16384 位
      key.Private_key = Convert.ToBase64String(Encoding.Default.GetBytes(rsa.ToXmlString(True)))
      key.Public_key = Convert.ToBase64String(Encoding.Default.GetBytes(rsa.ToXmlString(False)))
    End Using
    Return key
  End Function

  ''' <summary>
  ''' RSA加密
  ''' </summary>
  ''' <param name="PubKey"></param>
  ''' <param name="cont"></param>
  ''' <returns></returns>
  Public Shared Function RSAEncrypt(PubKey As String, cont As String) As String
    Dim en_cont As String = String.Empty
    Using rsa As New RSACryptoServiceProvider()
      rsa.FromXmlString(Encoding.Default.GetString(Convert.FromBase64String(PubKey)))
      Dim en_data As Byte() = rsa.Encrypt(Encoding.Default.GetBytes(cont), False)
      en_cont = Convert.ToBase64String(en_data)
    End Using
    Return en_cont
  End Function

  ''' <summary>
  ''' RSA解密
  ''' </summary>
  ''' <param name="PriKey"></param>
  ''' <param name="cont"></param>
  ''' <returns></returns>
  Public Shared Function RSADecrypt(PriKey As String, cont As String) As String
    Dim de_cont As String = String.Empty
    Using rsa As New RSACryptoServiceProvider()
      rsa.FromXmlString(Encoding.Default.GetString(Convert.FromBase64String(PriKey)))
      Dim decryptedData As Byte() = rsa.Decrypt(Convert.FromBase64String(cont), False)
      de_cont = Encoding.GetEncoding("GB2312").GetString(decryptedData)
    End Using
    Return de_cont
  End Function

  ''' <summary>
  ''' 私钥签名 
  ''' </summary>
  ''' <returns></returns>
  Public Shared Function RSA_Sign(prikey As String, KEY As String) As String
    Dim key_byt As Byte() = Encoding.UTF8.GetBytes(KEY)
    Dim RSA_SP As RSACryptoServiceProvider = New RSACryptoServiceProvider()
    RSA_SP.FromXmlString(Encoding.Default.GetString(Convert.FromBase64String(prikey)))
    Dim res As Byte() = RSA_SP.SignData(key_byt, "SHA1")
    Dim sign As String = Convert.ToBase64String(res)
    Return sign
  End Function

  ''' <summary>
  ''' 公钥验证 
  ''' </summary>
  ''' <returns></returns>
  Public Shared Function RSA_Verify(pubkey As String, KEY As String, sign As String) As Boolean
    Dim key_byt As Byte() = Encoding.UTF8.GetBytes(KEY)
    Dim RSA_SP As RSACryptoServiceProvider = New RSACryptoServiceProvider()
    RSA_SP.FromXmlString(Encoding.Default.GetString(Convert.FromBase64String(pubkey)))
    Dim sign_byt As Byte() = Convert.FromBase64String(sign)
    Dim Verify_bool As Boolean = RSA_SP.VerifyData(key_byt, "SHA1", sign_byt)
    Return Verify_bool
  End Function


  Public Class Create_key_type
    Public Property Public_key As String
    Public Property Private_key As String

  End Class
End Class
